package com.ocom.payconfig.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.ocom.common.AcpService;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.*;
import com.ocom.common.entity.company.EntityCompanyConfig;
import com.ocom.common.entity.company.EntityCompanyInfo;
import com.ocom.common.entity.master.EntityPayChannel;
import com.ocom.common.entity.pay.EntityAbnormal;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.GzhRefundQueryRequest;
import com.ocom.common.request.simple.GzhMsgRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.pay.ABCRechargeVo;
import com.ocom.common.vo.pay.UpdateListRechargeVo;
import com.ocom.common.vo.pay.UpdateSubsidyVo;
import com.ocom.payconfig.client.MqSimpleFeign;
import com.ocom.payconfig.client.PayhandleClientService;
import com.ocom.payconfig.client.XfRecordFeign;
import com.ocom.payconfig.service.*;
import com.ocom.payconfig.vo.GetListRechargeRVo;
import com.ocom.payconfig.vo.GetListRechargeVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class RechargeController {


    @Autowired
    private XfAccountInfoService xfAccountInfoService;


    @Autowired
    private XfRecordProcessService xfRecordProcessService;

    @Autowired
    private XfRecordFeign xfRecordFeign;

    @Autowired
    AbnormalService abnormalService;

    @Autowired
    private XfMerchantInfoService xfMerchantInfoService;

    @Autowired
    PayChannelService payChannelService;

    @Autowired
    private XfSubAccountListService xfSubAccountListService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    XfAccountTypeService xfAccountTypeService;

    @Autowired
    CompanyConfigService companyConfigService;

    @Autowired
    private PayhandleClientService payhandleClientService;

    @Autowired
    private MqSimpleFeign mqSimpleFeign;

    /**
     * 获取账号明细（getListRecharge）
     * */
    @PostMapping(PayConfigUrl.Url_GetListRecharge)
    public Result GetListRecharge(@RequestBody GetListRechargeVo getListRechargeVo){
        log.info("获取账号明细請求參數{{}}",getListRechargeVo);
        Map<String, Object> map = new HashMap<>();
        try {
            List<GetListRechargeRVo> getListRechargeRVos = xfAccountInfoService.GetListRecharge(getListRechargeVo.getComId(), getListRechargeVo);

            List<EntityXfMerchantInfo> list = xfMerchantInfoService.GetMerchInfo(getListRechargeVo.getComId());

            List<EntityPayChannel> payChannels = payChannelService.payList();

            for (GetListRechargeRVo getListRechargeRVo : getListRechargeRVos) {
                String signInfoIds="";
                if (getListRechargeRVo.getSignInfoIds()==null){
                    continue;
                }
                String[] signInfoIdsArr=getListRechargeRVo.getSignInfoIds().split(";");
                for (int j = 0; j < signInfoIdsArr.length; j++) {
                    if (j==0){
                        String[] signInfoIdsArrs=signInfoIdsArr[j].split("#");
                        for (int i = 0; i < signInfoIdsArrs.length; i++) {
                            for (EntityXfMerchantInfo entityXfMerchantInfo : list) {
                                Integer merchId= Integer.valueOf(signInfoIdsArrs[i]);
                                if (entityXfMerchantInfo.getMerchId().compareTo(merchId)==0){
                                    if (i == 0){
                                        signInfoIds= entityXfMerchantInfo.getMerchName()+"#";
                                    }
                                }
                            }
                            for (EntityPayChannel payChannel : payChannels) {
                                Integer payWay= Integer.valueOf(signInfoIdsArrs[i]);
                                if (payChannel.getPayWay().compareTo(payWay)==0){
                                    if (i == 1){
                                        signInfoIds=signInfoIds+ payChannel.getName();
                                    }
                                }
                            }
                        }
                    }else {
                        String[] signInfoIdsArrs=signInfoIdsArr[j].split("#");
                        for (int i = 0; i < signInfoIdsArrs.length; i++) {
                            for (EntityXfMerchantInfo entityXfMerchantInfo : list) {
                                Integer merchId= Integer.valueOf(signInfoIdsArrs[i]);
                                if (entityXfMerchantInfo.getMerchId().compareTo(merchId)==0){
                                    if (i == 0){
                                        signInfoIds= ";"+signInfoIds+entityXfMerchantInfo.getMerchName()+"#";
                                    }
                                }
                            }
                            for (EntityPayChannel payChannel : payChannels) {
                                Integer payWay= Integer.valueOf(signInfoIdsArrs[i]);
                                if (payChannel.getPayWay().compareTo(payWay)==0){
                                    if (i == 1){
                                        signInfoIds=signInfoIds+ payChannel.getName();
                                    }
                                }
                            }
                        }
                    }

                }
                getListRechargeRVo.setSignInfo(signInfoIds);
            }

            PageInfo<GetListRechargeRVo> getListRechargeRVoPageInfo = new PageInfo<>(getListRechargeRVos);

            if (getListRechargeVo.getSubAccount() > 0
                    && getListRechargeRVoPageInfo.getList() != null &&
                    getListRechargeRVoPageInfo.getList().size() > 0) {
                List<Long> pIds = getListRechargeRVoPageInfo.getList().stream().map(GetListRechargeRVo -> GetListRechargeRVo.getPId()).collect(Collectors.toList());
                List<EntityXfSubAccountList> xfSubAccountLists = xfSubAccountListService.getUserSubAccounts(getListRechargeVo.getComId(), pIds, getListRechargeVo.getSubAccount());
                List<GetListRechargeRVo> listRechargeRVos = getListRechargeRVoPageInfo.getList();
                for (GetListRechargeRVo getListRechargeRVo : listRechargeRVos) {
                    getListRechargeRVo.setAccountRechargeMoney(0l);
                    getListRechargeRVo.setAccountSubsidyMoney(0l);
                    for (EntityXfSubAccountList entityXfSubAccountList : xfSubAccountLists) {
                        if (entityXfSubAccountList.getPId().equals(getListRechargeRVo.getPId())) {
                            getListRechargeRVo.setAccountRechargeMoney(entityXfSubAccountList.getAccountRechargeMoney().longValue());
                            getListRechargeRVo.setAccountSubsidyMoney(entityXfSubAccountList.getAccountSubsidyMoney().longValue());
                            break;
                        }
                    }
                }
                getListRechargeRVoPageInfo.setList(listRechargeRVos);
            }

            map.put("total",getListRechargeRVoPageInfo.getTotal());
            map.put("list",getListRechargeRVoPageInfo.getList());
        }catch (Exception exception){
            log.error("获取账号明细失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"获取账号明细失败");
        }
        return ResultUtil.success(map);
    }

    /**
     * 个人账户充值（updateListRecharge）
     * */
    @PostMapping(PayConfigUrl.Url_UpdateListRecharge)
    public Result UpdateListRecharge(@RequestBody UpdateListRechargeVo updateListRechargeVo){
        log.info("个人账户充值請求參數  KVU2nsG1 :{{}}",updateListRechargeVo);

        String orderNo= "";
        if(updateListRechargeVo.getFrom() == 1){
            orderNo="PCZ"+DateUtils.getNowDate().getTime()+updateListRechargeVo.getPId();
        }else if(updateListRechargeVo.getFrom() == 2){
            orderNo="JK"+updateListRechargeVo.getOrderNo();
        }else{
            return ResultUtil.error(ResultEnum.Err_4040);
        }

        EntityXfAccountInfo entityXfAccountInfo = xfAccountInfoService.GetXFAccount(updateListRechargeVo.getComId(),updateListRechargeVo.getPId().intValue(),0);
        EntityXfSubAccountList entityXfSubAccountList= null;
        if (updateListRechargeVo.getSubAccount() == 0) {
            if (updateListRechargeVo.getRechargeMoney() < 0l) {
                if (entityXfAccountInfo.getAccountRechargeMoney() <= 0l) {
                    return ResultUtil.error(ResultEnum.Err_3201);
                }
                if (entityXfAccountInfo.getAccountRechargeMoney().compareTo(-1l * updateListRechargeVo.getRechargeMoney()) < 0) {
                    return ResultUtil.error(ResultEnum.Err_2040);
                }
            }
        }else{
            entityXfSubAccountList = xfSubAccountListService.getUserSubAccount(updateListRechargeVo.getComId(),updateListRechargeVo.getPId(),updateListRechargeVo.getSubAccount());
        }

        GetListRechargeVo getListRechargeVo = new GetListRechargeVo();
        getListRechargeVo.setPage(1);
        getListRechargeVo.setSize(1);
        getListRechargeVo.setComId(updateListRechargeVo.getComId());
        getListRechargeVo.setPId(updateListRechargeVo.getPId());
        List<GetListRechargeRVo> listRechargeRVos = xfAccountInfoService.GetListRecharge(updateListRechargeVo.getComId(),getListRechargeVo);

        GetListRechargeRVo rechargeRVo = listRechargeRVos.get(0);

        //待处理表
        EntityXfRecordProcess entityXfRecordProcess = new EntityXfRecordProcess();
        entityXfRecordProcess.setComId(updateListRechargeVo.getComId());
        entityXfRecordProcess.setDeptId(rechargeRVo.getDeptId());
        entityXfRecordProcess.setDeptName(rechargeRVo.getDeptName());
        entityXfRecordProcess.setPId(updateListRechargeVo.getPId());
        entityXfRecordProcess.setPCode(rechargeRVo.getPCode());
        entityXfRecordProcess.setPName(rechargeRVo.getPName());
        entityXfRecordProcess.setAccTypeId(entityXfAccountInfo.getAccTypeId());
        entityXfRecordProcess.setAccountNo(rechargeRVo.getAccountNo());
        entityXfRecordProcess.setSubBalance(rechargeRVo.getAccountSubsidyMoney());
        entityXfRecordProcess.setConsTime(DateUtils.getNowDate());
        entityXfRecordProcess.setConsMoney(updateListRechargeVo.getRechargeMoney());
        entityXfRecordProcess.setConsState(10);
        entityXfRecordProcess.setMealNo("0");
        entityXfRecordProcess.setMealDate(new Date());
        entityXfRecordProcess.setSubAccount(updateListRechargeVo.getSubAccount());
        entityXfRecordProcess.setMerchId(updateListRechargeVo.getMerchId());

        if (updateListRechargeVo.getMerchId() == 0l) {
            EntityCompanyInfo entityCompanyInfo = companyInfoService.GetCompanyInfo(updateListRechargeVo.getComId());
            if (entityCompanyInfo != null) entityXfRecordProcess.setMerchName(entityCompanyInfo.getComName());
        } else {
            EntityXfMerchantInfo entityXfMerchantInfo = xfMerchantInfoService.GetMerchInfo(updateListRechargeVo.getComId(), updateListRechargeVo.getMerchId());
            if (entityXfMerchantInfo != null) entityXfRecordProcess.setMerchName(entityXfMerchantInfo.getMerchName());
        }

        if (updateListRechargeVo.getSubAccount() == 0) {
            entityXfRecordProcess.setChargeBalance(entityXfAccountInfo.getAccountRechargeMoney() + updateListRechargeVo.getRechargeMoney());
        }else {
            entityXfRecordProcess.setChargeBalance(entityXfSubAccountList.getAccountRechargeMoney() + updateListRechargeVo.getRechargeMoney());
        }

        if(updateListRechargeVo.getRechargeMoney()<0l)  entityXfRecordProcess.setConsState(11);

        if(updateListRechargeVo.getFrom() == 1){
            entityXfRecordProcess.setOpType(11L);
            entityXfRecordProcess.setOpTypeDiff(1L);

            entityXfRecordProcess.setDeviceNo("MU"+updateListRechargeVo.getUserId());
            entityXfRecordProcess.setDeviceName(updateListRechargeVo.getUserName());
        }
        if(updateListRechargeVo.getFrom() == 2){
            entityXfRecordProcess.setOpType(14L);
            entityXfRecordProcess.setOpTypeDiff(12L);

            entityXfRecordProcess.setDeviceNo(updateListRechargeVo.getDockApiCode());
            entityXfRecordProcess.setDeviceName(updateListRechargeVo.getDockApiName());
        }
        entityXfRecordProcess.setOrderNo(orderNo);
        entityXfRecordProcess.setPayTime(DateUtils.getNowDate());
        entityXfRecordProcess.setPayState(0);
        entityXfRecordProcess.setMonthValue(DateUtils.getDate().replace("-","").substring(0,6));
        entityXfRecordProcess.setChargeMoney(updateListRechargeVo.getRechargeMoney());
        entityXfRecordProcess.setSubMoney(0L);
        entityXfRecordProcess.setRealMoney(updateListRechargeVo.getRechargeMoney());
        entityXfRecordProcess.setCreateTime(new Date());
        entityXfRecordProcess.setReserved1(updateListRechargeVo.getRemark());
        try {
            xfRecordProcessService.insertEntityXfRecord(entityXfRecordProcess.getComId(), entityXfRecordProcess);
        }catch (Exception exception){
            log.info("充值待处理添加失败:{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"充值待处理添加失败");
        }

        try {
            EntityXfRecordProcess updateXfRecordProcess=new EntityXfRecordProcess();
            updateXfRecordProcess.setPayState(1);
            updateXfRecordProcess.setPayTime(new Date());
            updateXfRecordProcess.setId(entityXfRecordProcess.getId());

            entityXfRecordProcess.setPayState(1);
            entityXfRecordProcess.setPayTime(updateXfRecordProcess.getPayTime());
            if (updateListRechargeVo.getSubAccount() == 0) {

                if (entityXfRecordProcess.getChargeMoney() == null && entityXfRecordProcess.getSubMoney() == null){
                    return ResultUtil.error(ResultEnum.Err_4134);
                }

                try {
                    xfAccountInfoService.updateAccountTransactional(entityXfAccountInfo.getComId(),entityXfAccountInfo.getId(), entityXfRecordProcess,updateXfRecordProcess);
                }catch (Exception e){
                    e.printStackTrace();
                    StackTraceElement ste =e.getStackTrace()[0];
                    log.info("请求方式59:【{}】,修改金额的事务:【{}】,异常的返回:【{}】,行号:【{}】","余额充值", entityXfRecordProcess,e,ste.getLineNumber());
                    return ResultUtil.error(ResultEnum.Err_4002);
                }
            }else {
                try {
                    xfSubAccountListService.updateAccountTransactional(entityXfAccountInfo.getComId(),entityXfAccountInfo.getId(), entityXfRecordProcess,updateXfRecordProcess);
                }catch (Exception e){
                    e.printStackTrace();
                    StackTraceElement ste =e.getStackTrace()[0];
                    log.info("请求方式599:【{}】,修改金额的事务:【{}】,异常的返回:【{}】,行号:【{}】","余额充值", entityXfRecordProcess,e,ste.getLineNumber());
                    return ResultUtil.error(ResultEnum.Err_4002);
                }
            }



            try {
                //同步订单到成功表
//                EntityXfRecord entityXfRecord = JSON.parseObject(JSON.toJSONString(entityXfRecordProcess), EntityXfRecord.class);
//                xfRecordFeign.sveXfRecord(entityXfRecord);
                Result result = payhandleClientService.SyncOrder(entityXfRecordProcess);
                if(result == null || result.getCode()!=200){
                    throw new RuntimeException(result.getMsg());
                }
            }catch (Exception exception){
                log.error("成功表插入失败!订单号：{{}}， {{}}", entityXfRecordProcess.getOrderNo(), exception.getMessage());
                EntityAbnormal entityAbnormal=new EntityAbnormal();
                entityAbnormal.setComId(entityXfAccountInfo.getComId());
                entityAbnormal.setPlatOrderNo(entityXfRecordProcess.getId().toString());
                entityAbnormal.setType(2);
                entityAbnormal.setRemark("mq迁移失败");
                abnormalService.Add(entityXfAccountInfo.getComId(),entityAbnormal);
            }
        }catch (Exception e){
            e.printStackTrace();
            StackTraceElement ste =e.getStackTrace()[0];
            log.info("请求方式60:【{}】,入库新增等待表的数据5:【{}】,异常的返回:【{}】,行号:【{}】","余额充值", entityXfRecordProcess,e,ste.getLineNumber());
            return ResultUtil.error(ResultEnum.Err_4002);
        }

        try {
            EntityCompanyConfig entityCompanyConfig = companyConfigService.getTapValue(entityXfRecordProcess.getComId(), "gzhMsg");
            if (entityCompanyConfig != null && entityCompanyConfig.getConfig().equals("true")) {
                GzhMsgRequest gzhMsgRequest = new GzhMsgRequest();
                gzhMsgRequest.setComId(entityXfRecordProcess.getComId());
                gzhMsgRequest.setMerchId(entityXfRecordProcess.getMerchId());
                gzhMsgRequest.setPId(entityXfRecordProcess.getPId());
                gzhMsgRequest.setOrderNo(entityXfRecordProcess.getOrderNo());
                gzhMsgRequest.getParams().setUserName(entityXfRecordProcess.getPName());
                gzhMsgRequest.getParams().setPayTime(DateUtil.getDateTime(entityXfRecordProcess.getConsTime()));
                gzhMsgRequest.getParams().setPayMoney(AcpService.stringAmount(entityXfRecordProcess.getConsMoney().toString()));
                gzhMsgRequest.getParams().setBalance(AcpService.stringAmount(entityXfRecordProcess.getChargeBalance().toString()));

                gzhMsgRequest.getParams().setAccTypeName("学生");
                EntityXfAccountType entityXfAccountType = xfAccountTypeService.getXFAccountType(entityXfRecordProcess.getComId(), entityXfAccountInfo.getAccTypeId());
                if (entityXfAccountType != null) {
                    gzhMsgRequest.getParams().setAccTypeName(entityXfAccountType.getAccTypeName());
                }
                mqSimpleFeign.gzhMsg(gzhMsgRequest);
            }
        } catch (Exception exception) {
            log.info("公众号消息发送失败:{{}}", exception.getMessage());
        }



        Map<String,Object>  resMap = new HashMap<>();
        resMap.put("chargeBalance", entityXfRecordProcess.getChargeBalance());
        return ResultUtil.success(resMap);
    }


    /**
     * 个人补贴充值（updateSubsidy）
     * */
    @PostMapping(PayConfigUrl.Url_UpdateSubsidy)
    public Result updateSubsidy(@RequestBody UpdateSubsidyVo updateSubsidyVo){
        log.info("个人补贴充值請求參數 smta2Sas :{{}}",updateSubsidyVo);

        String orderNo= "";
        if(updateSubsidyVo.getFrom() == 1){
            orderNo="CZ"+DateUtils.getNowDate().getTime()+updateSubsidyVo.getPId();
        }else if(updateSubsidyVo.getFrom() == 2){
            orderNo="JK"+updateSubsidyVo.getOrderNo();
        }else{
            return ResultUtil.error(ResultEnum.Err_4040);
        }
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        updateSubsidyVo.setComId(yoCiUser.getComId());

        EntityXfAccountInfo entityXfAccountInfo = xfAccountInfoService.GetXFAccount(yoCiUser.getComId(), updateSubsidyVo.getPId().intValue(),0);

        GetListRechargeVo getListRechargeVo = new GetListRechargeVo();
        getListRechargeVo.setPage(1);
        getListRechargeVo.setSize(1);
        getListRechargeVo.setComId(entityXfAccountInfo.getComId());
        getListRechargeVo.setPId(entityXfAccountInfo.getPId());
        List<GetListRechargeRVo> listRechargeRVos = xfAccountInfoService.GetListRecharge(entityXfAccountInfo.getComId(),getListRechargeVo);

        GetListRechargeRVo rechargeRVo = listRechargeRVos.get(0);

        EntityXfSubAccountList entityXfSubAccountList = null;
        if (updateSubsidyVo.getSubAccount() > 0) {
            entityXfSubAccountList = xfSubAccountListService.getUserSubAccount(updateSubsidyVo.getComId(), updateSubsidyVo.getPId(), updateSubsidyVo.getSubAccount());
        }

        //待处理表
        EntityXfRecordProcess entityXfRecordProcess = new EntityXfRecordProcess();
        entityXfRecordProcess.setComId(yoCiUser.getComId());
        entityXfRecordProcess.setDeptId(rechargeRVo.getDeptId());
        entityXfRecordProcess.setDeptName(rechargeRVo.getDeptName());
        entityXfRecordProcess.setPId(updateSubsidyVo.getPId());
        entityXfRecordProcess.setPCode(rechargeRVo.getPCode());
        entityXfRecordProcess.setPName(rechargeRVo.getPName());
        entityXfRecordProcess.setAccTypeId(entityXfAccountInfo.getAccTypeId());
        entityXfRecordProcess.setAccountNo(rechargeRVo.getAccountNo());
        entityXfRecordProcess.setChargeBalance( entityXfAccountInfo.getAccountRechargeMoney());
        entityXfRecordProcess.setSubBalance(rechargeRVo.getAccountSubsidyMoney()+updateSubsidyVo.getRechargeMoney());
        if (updateSubsidyVo.getSubAccount() > 0) {
            entityXfRecordProcess.setChargeBalance( entityXfSubAccountList.getAccountRechargeMoney().longValue());
            entityXfRecordProcess.setSubBalance(entityXfSubAccountList.getAccountSubsidyMoney().longValue()+updateSubsidyVo.getRechargeMoney());
        }
        entityXfRecordProcess.setConsTime(DateUtils.getNowDate());
        entityXfRecordProcess.setConsMoney(updateSubsidyVo.getRechargeMoney());
        entityXfRecordProcess.setConsState(12);
        entityXfRecordProcess.setMealNo("0");
        entityXfRecordProcess.setMealDate(new Date());
        entityXfRecordProcess.setSubAccount(updateSubsidyVo.getSubAccount());
        entityXfRecordProcess.setReserved1(updateSubsidyVo.getRemark());

        entityXfRecordProcess.setOpType(15L);
        if(updateSubsidyVo.getFrom() == 1){
            entityXfRecordProcess.setOpTypeDiff(11L);

            entityXfRecordProcess.setDeviceNo("MU"+yoCiUser.getId());
            entityXfRecordProcess.setDeviceName(yoCiUser.getUsername());
        }
        if(updateSubsidyVo.getFrom() == 2){

            entityXfRecordProcess.setOpTypeDiff(12L);

            entityXfRecordProcess.setDeviceNo(updateSubsidyVo.getDockApiCode());
            entityXfRecordProcess.setDeviceName(updateSubsidyVo.getDockApiName());
        }
        entityXfRecordProcess.setOrderNo(orderNo);
        entityXfRecordProcess.setPayTime(DateUtils.getNowDate());
        entityXfRecordProcess.setPayState(0);
        entityXfRecordProcess.setMonthValue(DateUtils.getDate().replace("-","").substring(0,6));
        entityXfRecordProcess.setChargeMoney(0l);
        entityXfRecordProcess.setSubMoney(updateSubsidyVo.getRechargeMoney());
        entityXfRecordProcess.setRealMoney(updateSubsidyVo.getRechargeMoney());
        entityXfRecordProcess.setCreateTime(new Date());
        entityXfRecordProcess.setReserved2(updateSubsidyVo.getImportLogId());
        entityXfRecordProcess.setCardNo(rechargeRVo.getCardNo());
        try {
            xfRecordProcessService.insertEntityXfRecord(entityXfRecordProcess.getComId(), entityXfRecordProcess);
        }catch (Exception exception){
            log.info("充值待处理添加失败:{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"充值待处理添加失败");
        }

        try {

            EntityXfRecordProcess updateXfRecordProcess=new EntityXfRecordProcess();
            updateXfRecordProcess.setPayState(1);
            updateXfRecordProcess.setPayTime(new Date());
            updateXfRecordProcess.setId(entityXfRecordProcess.getId());

            entityXfRecordProcess.setPayState(1);
            entityXfRecordProcess.setPayTime(updateXfRecordProcess.getPayTime());
            entityXfRecordProcess.setId(null);
            if (updateSubsidyVo.getSubAccount() == 0) {
                try {
                    if (entityXfRecordProcess.getChargeMoney() == null && entityXfRecordProcess.getSubMoney() == null){
                        return ResultUtil.error(ResultEnum.Err_4134);
                    }
                    xfAccountInfoService.updateAccountTransactional(entityXfAccountInfo.getComId(), entityXfAccountInfo.getId(), entityXfRecordProcess, updateXfRecordProcess);
                } catch (Exception e) {
                    e.printStackTrace();
                    StackTraceElement ste = e.getStackTrace()[0];
                    log.info("请求方式59:【{}】,修改金额的事务:【{}】,异常的返回:【{}】,行号:【{}】", "补贴充值", entityXfRecordProcess, e, ste.getLineNumber());
                    return ResultUtil.error(ResultEnum.Err_4002);
                }

            } else {
                try {
                    xfSubAccountListService.updateAccountTransactional(entityXfAccountInfo.getComId(), entityXfAccountInfo.getId(), entityXfRecordProcess, updateXfRecordProcess);
                } catch (Exception e) {
                    e.printStackTrace();
                    StackTraceElement ste = e.getStackTrace()[0];
                    log.info("请求方式599:【{}】,修改金额的事务:【{}】,异常的返回:【{}】,行号:【{}】", "余额充值", entityXfRecordProcess, e, ste.getLineNumber());
                    return ResultUtil.error(ResultEnum.Err_4002);
                }
            }

            try {
                //同步订单到成功表
                EntityXfRecord entityXfRecord = JSON.parseObject(JSON.toJSONString(entityXfRecordProcess), EntityXfRecord.class);
                xfRecordFeign.sveXfRecord(entityXfRecord);
            }catch (Exception exception){
                log.error("成功表更新失败!订单号：{{}}， {{}}", entityXfRecordProcess.getOrderNo(), exception.getMessage());
            }
        }catch (Exception e){
            e.printStackTrace();
            StackTraceElement ste =e.getStackTrace()[0];
            log.info("请求方式60:【{}】,入库新增等待表的数据5:【{}】,异常的返回:【{}】,行号:【{}】","补贴充值", entityXfRecordProcess,e,ste.getLineNumber());
            return ResultUtil.error(ResultEnum.Err_4002);
        }

        Map<String,Object>  resMap = new HashMap<>();
        resMap.put("subsidyBalance", entityXfRecordProcess.getSubBalance());
        return ResultUtil.success(resMap);
    }


    /**
     * 农行充值（updateListRecharge）
     * */
    @PostMapping(PayConfigUrl.Url_abcRecharge)
    public Result abcRecharge(@RequestBody ABCRechargeVo updateListRechargeVo){

        EntityXfAccountInfo entityXfAccountInfo = xfAccountInfoService.GetXFAccount(updateListRechargeVo.getComId(),updateListRechargeVo.getPId().intValue(),0);

        GetListRechargeVo getListRechargeVo = new GetListRechargeVo();
        getListRechargeVo.setPage(1);
        getListRechargeVo.setSize(1);
        getListRechargeVo.setComId(updateListRechargeVo.getComId());
        getListRechargeVo.setPId(updateListRechargeVo.getPId());
        List<GetListRechargeRVo> listRechargeRVos = xfAccountInfoService.GetListRecharge(updateListRechargeVo.getComId(),getListRechargeVo);

        GetListRechargeRVo rechargeRVo = listRechargeRVos.get(0);

        Date date=new Date();
        //待处理表
        EntityXfRecordProcess entityXfRecordProcess = new EntityXfRecordProcess();
        entityXfRecordProcess.setComId(updateListRechargeVo.getComId());
        entityXfRecordProcess.setDeptId(rechargeRVo.getDeptId());
        entityXfRecordProcess.setDeptName(rechargeRVo.getDeptName());
        entityXfRecordProcess.setPId(updateListRechargeVo.getPId());
        entityXfRecordProcess.setPCode(rechargeRVo.getPCode());
        entityXfRecordProcess.setPName(rechargeRVo.getPName());
        entityXfRecordProcess.setAccountNo(rechargeRVo.getAccountNo());
        entityXfRecordProcess.setChargeBalance( entityXfAccountInfo.getAccountRechargeMoney()+updateListRechargeVo.getRechargeMoney());
        entityXfRecordProcess.setSubBalance(rechargeRVo.getAccountSubsidyMoney());
        try {
            entityXfRecordProcess.setConsTime(DateUtils.getDate(updateListRechargeVo.getConsTime(),DateUtils.YYYY_MM_DD_HH_MM_SS) );
        } catch (ParseException e) {
            e.printStackTrace();
        }
        entityXfRecordProcess.setConsMoney(updateListRechargeVo.getRechargeMoney());
        entityXfRecordProcess.setConsState(10);
        entityXfRecordProcess.setOpType(12L);
        entityXfRecordProcess.setOpTypeDiff(205L);

        entityXfRecordProcess.setOrderNo(updateListRechargeVo.getOrderNo());

        entityXfRecordProcess.setPayTime(date);
        entityXfRecordProcess.setPayState(1);
        entityXfRecordProcess.setMonthValue(DateUtils.getDate().replace("-","").substring(0,6));
        entityXfRecordProcess.setChargeMoney(updateListRechargeVo.getRechargeMoney());
        entityXfRecordProcess.setRealMoney(updateListRechargeVo.getRechargeMoney());
        entityXfRecordProcess.setCreateTime(new Date());
        try {
            xfRecordProcessService.insertEntityXfRecord(entityXfRecordProcess.getComId(), entityXfRecordProcess);
        }catch (Exception exception){
            log.info("农行充值请求:【{}】,返回参数:【{}】", JSON.toJSONString(updateListRechargeVo),"充值待处理添加失败");
            log.info("充值待处理添加失败:{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"充值待处理添加失败");
        }
        try {
            Boolean i = xfAccountInfoService.abcRecharge(updateListRechargeVo.getComId(), updateListRechargeVo);
            if(!i){
                log.info("农行充值请求:【{}】,返回参数:【{}】", JSON.toJSONString(updateListRechargeVo),"充值失败");
                return ResultUtil.error(ResultEnum.Err_2009,"充值失败");
            }
        }catch (Exception exception){
            log.info("农行充值请求:【{}】,返回参数:【{}】", JSON.toJSONString(updateListRechargeVo),"充值失败");
            log.error("农行充值失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"充值失败");
        }

        //消费成功记录
        EntityXfRecord entityXfRecord = new EntityXfRecord();
        entityXfRecord.setComId(updateListRechargeVo.getComId());
        entityXfRecord.setDeptId(rechargeRVo.getDeptId());
        entityXfRecord.setDeptName(rechargeRVo.getDeptName());
        entityXfRecord.setPId(updateListRechargeVo.getPId());
        entityXfRecord.setPCode(rechargeRVo.getPCode());
        entityXfRecord.setPName(rechargeRVo.getPName());
        entityXfRecord.setAccountNo(rechargeRVo.getAccountNo());
        entityXfRecord.setChargeBalance(entityXfAccountInfo.getAccountRechargeMoney()+updateListRechargeVo.getRechargeMoney());
        entityXfRecord.setSubBalance(rechargeRVo.getAccountSubsidyMoney());
        try {
            entityXfRecord.setConsTime(DateUtils.getDate(updateListRechargeVo.getConsTime(),DateUtils.YYYY_MM_DD_HH_MM_SS) );
        } catch (ParseException e) {
            e.printStackTrace();
        }
        entityXfRecord.setConsMoney(updateListRechargeVo.getRechargeMoney());
        entityXfRecord.setConsState(10);
        entityXfRecord.setOpType(12L);
        entityXfRecord.setOpTypeDiff(205L);

        entityXfRecord.setOrderNo(updateListRechargeVo.getOrderNo());

        entityXfRecord.setPayTime(date);
        entityXfRecord.setPayState(1);
        entityXfRecord.setMonthValue(DateUtils.getDate().replace("-","").substring(0,6));
        entityXfRecord.setChargeMoney(updateListRechargeVo.getRechargeMoney());
        entityXfRecord.setCreateTime(date);
        entityXfRecord.setRealMoney(updateListRechargeVo.getRechargeMoney());

        xfRecordFeign.sveXfRecord(entityXfRecord);
        log.info("农行充值请求:【{}】,返回参数:【{}】", JSON.toJSONString(updateListRechargeVo),"充值成功");
        return ResultUtil.success();
    }

    @PostMapping(PayConfigUrl.Url_gzhRefunding)
    public Result<PageInfo<EntityXfRecordProcess>> gzhRefunding(@RequestBody GzhRefundQueryRequest request){
        return ResultUtil.success(xfRecordProcessService.selectGzhRechargePageInfo(request.getComId(),request));
    }


}
